package com.zhenzhen.xszhcp.controller;

import com.zhenzhen.xszhcp.model.Groupinfo;
import com.zhenzhen.xszhcp.model.Users;
import com.zhenzhen.xszhcp.service.GroupinfoService;
import com.zhenzhen.xszhcp.service.LogService;
import com.zhenzhen.xszhcp.service.MenuService;
import com.zhenzhen.xszhcp.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class JumpController {
    @Autowired
    private UsersService usersService;
    @Autowired
    private GroupinfoService groupinfoService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private LogService logService;

    /*主页*/
    @RequestMapping("/Index")
    public String index(Model model, HttpServletRequest httpServletRequest){
        HttpSession httpSession = httpServletRequest.getSession();
        Users users = (Users) httpSession.getAttribute("islogin");
        if (users!=null) {
            int groupid = users.getGroupid();
            String username = users.getUsername();
            String password = users.getUpassword();
            Users users1 = usersService.getusersByusername(username);
            if (users1.getUpassword().equals(password)){
//                此处封装需要带到前端的数据
                model.addAttribute("users",users1);
                Groupinfo groupinfo = groupinfoService.getGroupinfoById(users1.getGroupid());
                if (groupinfo!=null){
                    /*start权限中心*/
                    String quanxian = groupinfo.getQx();
                    String qx[] = {};
                    qx = quanxian.split(",");
                    Map map = new HashMap();
                    for (String qxcache : qx) {
                        int qxid = Integer.parseInt(qxcache);
                        Map map1 = new HashMap();
                        map1.put("pmenu", menuService.selectByPrimaryKey(qxid));
                        map1.put("cmenu", menuService.getmenubyfdm(qxid));
                        map.put(String.valueOf(qxid) + "menus", map1);
                    }
                    model.addAttribute("parentmenus", map);
                    /*end权限中心*/
                    return "index";
                }
                else {
                    model.addAttribute("errmsg","用户组信息错误！");
                    return "login";
                }
            }
            else {
                model.addAttribute("errmsg","您已更改密码请重新输入");
                return "login";
            }
        }
        return "login";
    }

    /*登录*/
    @RequestMapping("/Login")
    public String Login(@RequestParam String username, @RequestParam String password, Model model, HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
        password = UtilPacket.Md5MD5String(password);//传入用户输入的密码进行MD5加密
        if (username.equals("admin")&&password.equals(UtilPacket.Md5MD5String("admin"))){
            Groupinfo groupinfo = groupinfoService.getGroupinfoById(1);
            if (groupinfo!=null) {
                /*start权限中心*/
                String quanxian = groupinfo.getQx();
                String qx[] = {};//定义一个可变的数组，名称为qx
                qx = quanxian.split(",");//将字符串分割为字符数组
                Map map = new HashMap();
                for (String qxcache : qx) {//for (循环变量类型 循环变量名称 : 要被遍历的对象) 循环体
                    int qxid = Integer.parseInt(qxcache);//将qxcache中信息转换为int类型
                    Map map1 = new HashMap();
                    map1.put("pmenu", menuService.selectByPrimaryKey(qxid));//查找menu表中id的菜单
                    map1.put("cmenu", menuService.getmenubyfdm(qxid));//查找menu表中父代码为此di的菜单
                    map.put(String.valueOf(qxid) + "menus", map1);//将qxid转换为string类型
                }
                model.addAttribute("parentmenus", map);
                /*end权限中心*/
            }
            Users users = new Users();
            users.setUsername("admin");
            model.addAttribute("users",users);
            setLog.setlod(httpServletRequest,"admin登录",logService);
            return "index";
        }
        else if(usersService.getusersByusername(username)!=null){
            if (usersService.getusersByusername(username).getUpassword().equals(password))
            {
                Users users = usersService.getusersByusername(username);
                model.addAttribute("users",users);
                session.setAttribute("islogin",users);
                int groupid = users.getGroupid();
                Groupinfo groupinfo = groupinfoService.getGroupinfoById(groupid);
                if (groupinfo!=null){
                    /*start权限中心*/
                    String quanxian = groupinfo.getQx();
                    String qx[] = {};
                    qx = quanxian.split(",");
                    Map map = new HashMap();
                    for (String qxcache : qx) {
                        int qxid = Integer.parseInt(qxcache);
                        Map map1 = new HashMap();
                        map1.put("pmenu", menuService.selectByPrimaryKey(qxid));
                        map1.put("cmenu", menuService.getmenubyfdm(qxid));
                        map.put(String.valueOf(qxid) + "menus", map1);
                    }
                    model.addAttribute("parentmenus", map);
                    /*end权限中心*/
                    session.setAttribute("islogin",users);
                    setLog.setlod(httpServletRequest,"id为"+users.getId()+"的用户登录",logService);
                    return "index";
                }
                model.addAttribute("errmsg","用户组信息错误！");
                return "login";
            }
            else {
                model.addAttribute("errmsg","密码错误！");
                return "login";
            }
        }
        else {
            model.addAttribute("errmsg","用户名或密码输入错误！");
            return "login";
        }
    }

    /*跳转*/
    @RequestMapping("/Jumpto")
    public String Jumpto(@RequestParam("url") String url){
        return url;
    }

    /*设置密码*/
    @RequestMapping("/szmm")
    public String szmm(HttpServletRequest httpServletRequest,Model model){
        HttpSession session = httpServletRequest.getSession();
        if (session.getAttribute("islogin")!=null)
            return "table/szmm";
        else
        {
            model.addAttribute("errmsg","请先登录！");
            return "login";
        }
    }

    /*设置密码*/
    @RequestMapping("/resetpassword")
    public String resetpassword()
    {return  "table/xgmm";}

    /*登出*/
    @RequestMapping("/logout")
    public String logout(HttpServletRequest httpServletRequest){
        HttpSession httpSession = httpServletRequest.getSession();
        setLog.setlod(httpServletRequest, "安全退出",logService);
        httpSession.invalidate();
        return "logout";
    }
}
